package com.baidu.foodrecipe.ui.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.baidu.foodrecipe.data.model.Result
import com.baidu.foodrecipe.databinding.LoadMoreItemBinding
import com.baidu.foodrecipe.databinding.RecipeItemBinding
import com.baidu.foodrecipe.ui.bindingEvents.ClickEvents
import com.baidu.foodrecipe.ui.fragment.recipes.MainViewModel

class RecipesAdapter : RecyclerView.Adapter<RecipesAdapter.MyViewHolder>() {
    private var itemList = emptyList<Result>()
    var mainViewModel: MainViewModel? = null
    var lifecycleOwner: LifecycleOwner? = null

    companion object {
        const val NORMAL_RECIPE_ITEM = 1
        const val LOAD_MORE_ITEM = 2
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): MyViewHolder {
        val inflater = LayoutInflater.from(parent.context)

        return if (viewType == NORMAL_RECIPE_ITEM) {
            val recipeItemBinding = RecipeItemBinding.inflate(inflater)
            MyViewHolder(recipeItemBinding, NORMAL_RECIPE_ITEM)
        } else {
            val loadMoreItemBinding = LoadMoreItemBinding.inflate(inflater)
            MyViewHolder(loadMoreItemBinding, LOAD_MORE_ITEM)
        }
    }

    override fun onBindViewHolder(
        holder: MyViewHolder,
        position: Int
    ) {
        if (position < itemList.size) {
            holder.bindNormalItem(itemList[position])
        } else {
            holder.bindLoadMoreItem(mainViewModel, lifecycleOwner)
        }
    }

    //获取item的数量 + 加载更多的item
    override fun getItemCount(): Int {
        return if (itemList.isNotEmpty()) {
            itemList.size + 1
        } else {
            0
        }
    }

    //获取每个对应item的类型
    override fun getItemViewType(position: Int): Int {
        //只有最后一个item才返回1 表示加载更多
        return if (position < itemList.size) {
            NORMAL_RECIPE_ITEM
        } else {
            LOAD_MORE_ITEM
        }
    }

    class MyViewHolder(val binding: ViewBinding, val type: Int) :
        RecyclerView.ViewHolder(binding.root) {
        fun bindNormalItem(result: Result) {
            if (type == NORMAL_RECIPE_ITEM) {
                binding as RecipeItemBinding
                binding.itemModel = result
                binding.events = ClickEvents()
                binding.isFavoritePage = false
                binding.executePendingBindings()
            }
        }

        fun bindLoadMoreItem(
            viewModel: MainViewModel? = null,
            lifecycleOwner: LifecycleOwner? = null
        ) {
            if (type == LOAD_MORE_ITEM) {
                binding as LoadMoreItemBinding
                binding.root.context
                if (viewModel != null && lifecycleOwner != null) {
                    viewModel.showLoadAnimation.observe(lifecycleOwner) {
                        if (it) {
                            binding.progressBar.visibility = View.VISIBLE
                        } else {
                            binding.progressBar.visibility = View.INVISIBLE
                        }
                    }
                }
                binding.root.setOnClickListener {
                    viewModel?.let {
                        viewModel.getMoreRecipes()
                    }
                }
            }
        }
    }

    fun setNewData(newItems: List<Result>) {
        //使用DiffUtil去分析新旧数据是否相同
        //通过CallBack抽象类来告诉DiffUtil我是如何判断相同和不相同的规则的
        val diffResult = DiffUtil.calculateDiff(DiffCallBackImpl(itemList, newItems))
        //修改数据源
        itemList = newItems
        //使用diffUtil去刷新Adapter
        diffResult.dispatchUpdatesTo(this)
    }

    private class DiffCallBackImpl(
        val itemList: List<Result>,
        val newItems: List<Result>
    ) : DiffUtil.Callback() {

        override fun getOldListSize(): Int {
            return itemList.size
        }

        override fun getNewListSize(): Int {
            return newItems.size
        }

        override fun areItemsTheSame(
            oldItemPosition: Int,
            newItemPosition: Int
        ): Boolean {
            //比较两个对象是否相同
            //java中使用 == 来判断两个对象是否相同
            //kotlin中使用 ===
            return itemList[oldItemPosition] === newItems[newItemPosition]
        }

        override fun areContentsTheSame(
            oldItemPosition: Int,
            newItemPosition: Int
        ): Boolean {
            //比较两个对象的内容是否相同
            //java中使用 equals 来判断两个对象的内容是否相同
            //kotlin中使用 ==
            return itemList[oldItemPosition] == newItems[newItemPosition]
        }
    }
}














